package cn.gqr.service.Impl;

import cn.gqr.entity.ItemBook;
import cn.gqr.entity.OrdInfo;
import cn.gqr.entity.Order;
import cn.gqr.entity.OrderLine;
import cn.gqr.mapper.OrderMapper;
import cn.gqr.service.OrderService;
import cn.gqr.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper OrderMapper;

    //后端

    @Override
    public int addShip(Order ord) {
        return OrderMapper.addShip(ord);
    }

    @Override
    public Map<String,Object> findOrdInfo(OrderLine orderLine,Integer ispay,String tstart , String tend, int start, int num) {
        Map map = new HashMap();
        map.put("total",OrderMapper.ordInfoTotal(orderLine,ispay,tstart,tend,start,num));
        map.put("orders",OrderMapper.findOrdInfo(orderLine,ispay,tstart,tend,start,num));
        return RespJson.pack(200,"",map);
    }


    @Override
    public List<ItemBook> findItemInfoByOrdid(int ordid) {
        return OrderMapper.findItemInfoByOrdid(ordid);
    }



    //前端

    @Override
    public OrdInfo findInfoByOrdid(int userid,int ordid) {
        return OrderMapper.findInfoByOrdid(userid,ordid);
    }

    @Override
    public int addAddr(Order order) {
        return OrderMapper.addAddr(order);
    }

    @Override
    public int addPay(Order order) {
        order.setTpay(new Date());
        return OrderMapper.addPay(order);
    }

    @Override
    public List<OrderLine> findOrdLines(int uid,Integer ispay,String tstart,String tend) {
        return OrderMapper.findOrdLines(uid,ispay,tstart,tend);
    }

    @Override
    public Map<String,Object> findOrdLineByPage(int uid, Integer ispay, String tstart, String tend, int start, int num) {
        System.out.println(tstart+"----"+tend);
        //获取分页订单头信息
        List<OrderLine> ordLineByPage = OrderMapper.findOrdLineByPage(uid, ispay, tstart, tend, start, num);
        if(ordLineByPage.isEmpty()){
            return  RespJson.pack(500,"查询数据有误",null);
        }
        Map map = new HashMap();
        map.put("total",OrderMapper.findOrdLineByPageToral(uid));
        //获取全部订单的总行数
        map.put("orders",ordLineByPage);
        return RespJson.pack(200,"",map);
    }

    @Override
    public Order findByIdOrder(int ordid) {
        return OrderMapper.findByIdOrder(ordid);
    }
}
